package cn.com.jrj.app;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 注册配置文件中的全部外部应用，并且执行外部应用的初始化和销毁。
 * 
 * @author 
 * 
 */
public class VnetApplication {
	/** 全部外部应用的容器，key为配置文件中的应用的名字value为应用对象 * */
	Map<String, Application> map = new HashMap<String, Application>();

	public VnetApplication() {
	}

	/**
	 * 销毁全部外部应用占用的资源
	 * 
	 */
	public void close() {
		Iterator<String> it = map.keySet().iterator();

		while (it.hasNext()) {
			String key = it.next();
			Application app = (Application) map.get(key);
			app.close();
		}
		map.clear();
	}

	/**
	 * 初始化全部外部应用
	 * 
	 * @throws Exception
	 */
	public void startup() throws Exception {
		Application[] apps = { new PublishApplication() };
		for (int i = 0; i < apps.length; i++) {
			apps[i].startup();
			map.put(apps.getClass().getName(), apps[i]);
		}
	}

	public Application getApplication(String name) {
		return (Application) map.get(name);
	}

	public static void main(String[] args) throws Exception {
		VnetApplication apps = new VnetApplication();
		apps.startup();
	}
}
